﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Agresso.Interface.Layer.Domain;
using Agresso.Interface.Layer.Service;
using Gizmox.WebGUI.Forms;
using System.Collections;

namespace Agresso.Interface.Layer.Web
{
    public class ListViewTools
    {
        private ContextMenuStrip contextMenuStrip1;
        private ToolStripMenuItem toolStripMenuItem2;
        User m_current_user;
        ListView m_listView;
           
        public ListViewTools(ListView lv, User user)
        {
            m_current_user = user;
            m_listView = lv;
            
            this.contextMenuStrip1 = new Gizmox.WebGUI.Forms.ContextMenuStrip();
            this.toolStripMenuItem2 = new Gizmox.WebGUI.Forms.ToolStripMenuItem();

            this.toolStripMenuItem2.Name = "toolStripMenuItem2";
            this.toolStripMenuItem2.Padding = new Gizmox.WebGUI.Forms.Padding(0, 0, 0, 0);
            this.toolStripMenuItem2.Size = new System.Drawing.Size(152, 20);
            this.toolStripMenuItem2.Text = "Column";
            this.toolStripMenuItem2.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.toolStripMenuItem2.Click += new EventHandler(toolStripMenuItem2_Click);

            this.contextMenuStrip1.Anchor = ((Gizmox.WebGUI.Forms.AnchorStyles)((Gizmox.WebGUI.Forms.AnchorStyles.Top | Gizmox.WebGUI.Forms.AnchorStyles.Left)));
            this.contextMenuStrip1.BorderWidth = new Gizmox.WebGUI.Forms.BorderWidth(1);
            this.contextMenuStrip1.DockPadding.Bottom = 2;
            this.contextMenuStrip1.DockPadding.Left = 1;
            this.contextMenuStrip1.DockPadding.Right = 1;
            this.contextMenuStrip1.DockPadding.Top = 2;
            this.contextMenuStrip1.Items.AddRange(new Gizmox.WebGUI.Forms.ToolStripItem[] {
            this.toolStripMenuItem2});
            this.contextMenuStrip1.Name = "contextMenuStrip1";
            this.contextMenuStrip1.Size = new System.Drawing.Size(100, 25);

            m_listView.ContextMenuStrip = contextMenuStrip1;
        }

        void toolStripMenuItem2_Click(object sender, EventArgs e)
        {
            ListViewColumnForm p = new ListViewColumnForm(m_listView, m_current_user);
            p.ShowDialog();
        }
        public static void LoadConfiguration(ListView lv, User user)
        {
            ConfigurationSettingService css = new ConfigurationSettingService();
            ConfigurationSetting cs = css.GetConfigurationSetting(lv.Parent.Name + user.UserID.ToString());
            if (cs == null)
            {
                cs = new ConfigurationSetting();
                cs.setting_name = lv.Parent.Name + user.UserID.ToString();
                string colwidth = "";
                string colhide = "";
                for (int i = 0; i < lv.Columns.Count; i++)
                {
                    colwidth += lv.Columns[i].Width.ToString() + ",";
                    colhide += lv.Columns[i].Visible.ToString() + ",";
                }
                cs.val_12 = colwidth;
                cs.val_13 = colhide;
                css.InsertConfiguration(cs);
            }
            else
            {
                string[] colwidth = cs.val_12.Split(',');
                string[] colhide = cs.val_13.Split(',');
                for (int i = 0; i < lv.Columns.Count; i++)
                {
                    try
                    {
                        lv.Columns[i].Width = Convert.ToInt16(colwidth[i]);
                    }
                    catch { }
                    try
                    {
                        lv.Columns[i].Visible = Convert.ToBoolean(colhide[i]);
                    }
                    catch { }
                }
            }
        }
        public static void SaveConfiguration(ListView lv, User user)
        {
            ConfigurationSettingService css = new ConfigurationSettingService();
            ConfigurationSetting cs = css.GetConfigurationSetting(lv.Parent.Name + user.UserID.ToString());
            if (cs == null)
            {
                cs = new ConfigurationSetting();
                cs.setting_name = lv.Parent.Name + user.UserID.ToString();
            }
            string colwidth = "";
            string colhide = "";
            for (int i = 0; i < lv.Columns.Count; i++)
            {
                colwidth += lv.Columns[i].Width.ToString() + ",";
                colhide += lv.Columns[i].Visible.ToString() + ",";
            }
            cs.val_12 = colwidth;
            cs.val_13 = colhide;
            if(cs.id==0)
                css.InsertConfiguration(cs);
            else
                css.UpdateConfiguration(cs);
        }
    }
}